/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */
package l1j.server.server.clientpackets;

import l1j.opqlo.NewSystem.各項記錄輸出;
import l1j.opqlo.Server.L1World.L1World;

import java.sql.Timestamp;//TODO 記錄文件檔 by 阿傑

import l1j.server.echo.ClientThread;
import l1j.server.server.model.Instance.L1ItemInstance;
import l1j.server.server.model.Instance.L1NpcInstance;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.model.Instance.L1PetInstance;
import l1j.server.server.serverpackets.S_ServerMessage;
import l1j.server.server.serverpackets.S_SystemMessage;

import java.util.logging.Level;
import java.util.logging.Logger;
/**
 * 處理收到由客戶端傳來丟道具到地上的封包
 */
public class C_DropItem extends ClientBasePacket {
	private static final String C_DROP_ITEM = "[C] C_DropItem";
	private static final Logger _log = Logger.getLogger(C_DropItem.class.getName());
	@Override
	public String getType() {
		return C_DROP_ITEM;
	}
	@Override
	public void start(final byte[] decrypt, final ClientThread client) {
		try {
			// 資料載入
			this.read(decrypt);

			final L1PcInstance pc = client.getActiveChar();
			if (pc == null) {
				return;
			}
			final int x = this.readH();
			final int y = this.readH();
			final int objectId = this.readD();
			int count = this.readD();

			//opqlo 防洗
			count = Math.abs(count);
			count = Math.min(count,2000000000);
			//opqlo 防洗
			if (pc.isGhost()) {
				return;
			}else if (pc.getMapId() >= 16384 && pc.getMapId() <= 25088) { // 旅館內判斷
				pc.sendPackets(new S_ServerMessage(539)); // \f1你無法將它放在這。
				return;
			}

			final L1ItemInstance item = pc.getInventory().getItem(objectId);

			//opqlo 防洗
			if(item == null){
				return;
			}
			if(count > 0 && count < 2000000000
					&& item.getCount() > 0 && item.getCount() >= count)
			{
				//opqlo 防洗

				if (item != null) {
					final boolean isGM = pc.isGm();
					if (!isGM && item.getDeleteDate() != null) { // 道具天數刪除系統(不可丟棄)
						pc.sendPackets(new S_ServerMessage(210, item.getItem().getName()));
						return;
					}
					if (!isGM && !item.getItem().isTradable()) {
						// \f1%0は捨てたりまたは他人に讓ることができません。
						pc.sendPackets(new S_ServerMessage(210, item.getItem().getName()));
						return;
					}
					if (item.isEquipped()) {
						// \f1削除できないアイテムや装備しているアイテムは捨てられません。
						pc.sendPackets(new S_ServerMessage(125));
						return;
					}
					if (!isGM && item.getBless() >= 128) { // 封印された装備
						// \f1%0は捨てたりまたは他人に讓ることができません。
						pc.sendPackets(new S_ServerMessage(210, item.getItem().getName()));
						return;
					}
					for (final L1NpcInstance petNpc : pc.getPetList().values()) {
						if (petNpc instanceof L1PetInstance) {
							final	L1PetInstance pet = (L1PetInstance) petNpc;
							if (item.getId() == pet.getItemObjId()) {
								// \f1%0は捨てたりまたは他人に讓ることができません。
								pc.sendPackets(new S_ServerMessage(210, item.getItem().getName()));
								return;
							}
						}
					}

					//TODO 魔法娃娃使用判斷無法丟棄
				/*	for (final L1DollInstance doll : pc.getDollList().values()) {
						if (doll.getItemObjId() == item.getId()) {
							pc.sendPackets(new S_ServerMessage(1181));
							return;
						}
					}*/
					if(pc.getDollList().getDollByItem(item.getId()) !=null){
						pc.sendPackets(new S_ServerMessage(1181));
						return;
					}
					//TODO 魔法娃娃使用判斷無法丟棄

					StringBuilder aa=new StringBuilder();
					//TODO 丟棄物品記錄bybill00148

					aa.append("IP").append(pc.getNetConnection().getIp().toString()).append(")玩家:【").append(pc.getName()).append("】	丟棄 【").append(item.getViewName())
					.append("】		數量:").append(count).append("流水號:").append(item.getId()).append("]	--時間:(").append(new Timestamp(System.currentTimeMillis())).append(")。");
					各項記錄輸出.LogSave(26, aa.toString());
					aa.delete(0, aa.length());
					aa = null;
					//TODO 丟棄物品記錄bybill00148

					// TODO 物品丟地上系統會自動刪除 by bill00148
					if (!pc.isGm()) {
						pc.getInventory().removeItem(objectId, count);
						pc.sendPackets(new S_SystemMessage("請愛護地球，垃圾不亂丟世界更美麗。"));
						return;
					}
					pc.getInventory().tradeItem(item, count,L1World.getInstance().getInventory(x, y, pc.getMapId()));
					pc.turnOnOffLight();
				}
				//opqlo 防洗
			} else {
				_log.info("[c]DropItem 防洗踢除 "+client.getAccountName());
				client.close();
				return;
			}
			//opqlo 防洗
		} catch (final Exception e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);

		} finally {
			this.over();
		}
	}
}
